<?php
/**
 * Created by allen.qiang.
 * Date: 2016-05-03
 * Time: 15:22
 * 处理各种订单非正常业务逻辑
 */
quanli_include('module/base/customer/logHandler.class.php');
class ajax_billTools {

    public function execute() {

        if (isset($_REQUEST['operFlg']) && quanli_is_int($_REQUEST['operFlg'])) {

            switch($_REQUEST['operFlg']) {

                case 1: //订单状态更新
                    $this->update_bill_status();
                    break;

                default:
                    returnJsonData('参数错误');
                    break;
            }
        } else {
            returnJsonData('参数错误');
        }
    }

    /*
     * 订单状态更新处理
     */
    private function update_bill_status() {
        global $G_PGLOB;
        //查询订单
        $bill_master = new entity('bill_master');
        $bill_master->bill_code = $_REQUEST['bill_code'];
        if (!$G_PGLOB->DBCA->fetch($bill_master)){
            returnJsonData('订单信息不存在', 404);
        }
        if ($bill_master->bill_status ==  $_REQUEST['bill_status']){
            returnJsonData('订单状态已经是将要更新的状态，无需更新', 404);
        }

        $bill_master->bill_status = $_REQUEST['bill_status'];
        $G_PGLOB->DBCA->save($bill_master);

        //记录订单轨迹
        $this->addBillTrace( $bill_master->bill_no, $G_PGLOB->currentUserID, '您的订单状态已更新为 ' . $GLOBALS['backend_bill_status'][$_REQUEST['bill_status']]);

        //订单日志参数
        $log_array = array(
            'logCategoryID' => 1,
            'logItem' => $bill_master->bill_no,
            'extend_1' => '人工改状态',
            'extend_5' => $_REQUEST['bill_status'],
            'operator' => $G_PGLOB->currentUserName
        );

        //实例日志类
        $logHandler = new logHandler( $G_PGLOB->DBCA );
        //保存订单日志
        $result_array = $logHandler->saveLogInfo( $log_array );
        if ($result_array['code'] != 200) {
            error_log('订单日志保存出错，请重试');
        }

        returnJsonData('订单状态更新成功！',200);
    }
    /**
     * 保存订单轨迹
     * @param int $bill_no 订单号
     * @param string $operator 操作人
     * @param string $content 轨迹内容
     * @return bool
     */
    private function addBillTrace( $bill_no, $operator, $content ) {

        global $G_SHOP;

        $bill_trace = new entity('bill_trace');
        $bill_trace->creator = $operator;
        $bill_trace->bill_no = $bill_no;
        $bill_trace->content = $content;
        $G_SHOP->DBCA->save( $bill_trace );

        return true;

    }


}